package com.joe.blockview.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;

import com.joe.blockview.R;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class BlockStartView extends BlockView {
    public BlockStartView(Context context) {
        super(context);
    }

    public BlockStartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BlockStartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mBlockViewSize.x = selfWidth;
        mBlockViewSize.y = selfHeight;
        setMeasuredDimension(mBlockViewSize.x, mBlockViewSize.y);
    }


    @Override
    public void init() {
        super.init();
        connection.setType(0);
        selfHeight = 150;
        selfWidth = 345;

        setCommand("Start");
        setName("Start");
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.rgb(92, 186, 122));

        //添加路径
        Path path1 = new Path();
        path1.moveTo(10, 0);
        path1.lineTo(200, 0);
        path1.addArc(135, 0, 265, 130, -90, 180);
        path1.lineTo(200, 130);
        path1.lineTo(100, 130);
        path1.lineTo(80, 150);
        path1.lineTo(60, 150);
        path1.lineTo(40, 130);
        path1.lineTo(10, 130);
        path1.arcTo(0, 110, 20, 130, 90, 90, false);
        path1.lineTo(0, 10);
        path1.arcTo(0, 0, 20, 20, 180, 90, false);

        //填充内部颜色
        canvas.drawPath(path1, paint);


        //绘制边界
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.rgb(56, 142, 82));
        paint.setStrokeWidth(2);
        canvas.drawPath(path1, paint);


        //添加图标
        Drawable d = getResources().getDrawable(R.drawable.block_start_ic);
        Bitmap bp = drawableToBitmap(d);
        canvas.drawBitmap(bp, 140f, 25f, paint);
//
//        Drawable d2 = getResources().getDrawable(R.drawable.ic_foward);
//        Bitmap bp2 = drawableToBitmap(d2);
//        canvas.drawBitmap(bp2, 0f, 0f, paint);
    }

    //drawable转bitmap
    private Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(80, 80, config);// 建立对应bitmap
        Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布
        drawable.setBounds(0, 0, 80, 80);
        drawable.draw(canvas);// 把drawable内容画到画布中
        return bitmap;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }
}
